package com.pb.service.boot.config;

import com.pb.service.boot.converter.MyMessageConverter;
import com.plumelog.core.PlumeLogTraceIdInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.web.accept.HeaderContentNegotiationStrategy;
import org.springframework.web.accept.ParameterContentNegotiationStrategy;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.servlet.config.annotation.*;
import org.springframework.web.util.UrlPathHelper;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 配置
 *
 * @Author:chenjian
 * @Date: 2019/11/28
 */
@Configuration
@EnableWebMvc
public class WebConfigurer implements WebMvcConfigurer {

    /**
     * 注册拦截器
     *
     * @param registry
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new FileHeaderCheckInterceptor())
                .addPathPatterns("/**");
        /*plumelog给每个请求添加追踪码*/
        registry.addInterceptor(new PlumeLogTraceIdInterceptor())
                .addPathPatterns("/**");
    }
    /**
     * 自定义内容协商策略和开启矩阵参数
     * @Author:廖昊源
     * @param
     */
    @Bean
    public WebMvcConfigurer webMvcConfigurer(){
        return new WebMvcConfigurer() {
            /**
             * 自定义内容协商策略
             * @param configurer
             */
            @Override
            public void configureContentNegotiation(ContentNegotiationConfigurer configurer) {
                Map<String, MediaType> map=new HashMap<String, MediaType>();
                //指定支持解析那些参数对应那些类型的媒体集
                map.put("json", MediaType.APPLICATION_JSON);
                map.put("xml", MediaType.APPLICATION_XML);
                map.put("xlsx", MediaType.parseMediaType("application/xlsx;charset=UTF-8"));
                ParameterContentNegotiationStrategy parameterStrategy = new ParameterContentNegotiationStrategy(map);
                HeaderContentNegotiationStrategy headerStrategy = new HeaderContentNegotiationStrategy();
                configurer.strategies(Arrays.asList(parameterStrategy,headerStrategy));
            }

            @Override
            public void extendMessageConverters(List<HttpMessageConverter<?>> converters) {
                converters.add(new MyMessageConverter());
            }
            @Override
            public void configurePathMatch(PathMatchConfigurer configurer) {
                UrlPathHelper urlPathHelper = new UrlPathHelper();
                //不移除分号“；”后面的参数内容，矩阵变量功能就可以生效
                urlPathHelper.setRemoveSemicolonContent(false);
                configurer.setUrlPathHelper(urlPathHelper);
            }


        };
    }

    /**
     * 自定义参数解析器
     * @param resolvers
     */
    @Override
    public void addArgumentResolvers(List<HandlerMethodArgumentResolver> resolvers) {
        resolvers.add(new ImportExcelArgumentResolver());
    }
}
